package com.yserp.project.erp.store.inorder.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.record.checkorderlog.domain.CheckOrderLog;
import com.yserp.project.erp.store.inorder.domain.InOrder;
import com.yserp.project.erp.store.inorder.domain.InOrderDto;
import com.yserp.project.erp.store.inorder.domain.InOrderVo;
import com.yserp.project.erp.store.inorder.service.IInOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品入库单主Controller
 *
 * @author yserp
 * @date 2022-07-01
 */
@Controller
@RequestMapping("/store/inorder")
public class InOrderController extends BaseController {
    private String prefix = "erp/store/inorder";

    @Autowired
    private IInOrderService inOrderService;

    @RequiresPermissions("store:inorder:view")
    @GetMapping()
    public String inorder() {
        return prefix + "/inorder";
    }

    /**
     * 查询商品入库单主列表
     */
    @RequiresPermissions("store:inorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InOrder inOrder) {
        startPage();
        List<InOrder> list = inOrderService.selectInOrderList(inOrder);
        return getDataTable(list);
    }

    /**
     * 导出商品入库单主列表
     */
    @RequiresPermissions("store:inorder:export")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InOrder inOrder) {
        List<InOrder> list = inOrderService.selectInOrderList(inOrder);
        ExcelUtil<InOrder> util = new ExcelUtil<InOrder>(InOrder.class);
        return util.exportExcel(list, "商品入库单主数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.IN_ORDER.getPref()));
    }

    /**
     * 新增商品入库单主
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("inOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.IN_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 采购一键入库
     * @param id 采购单主键ID
     * @param mmap 装数据的容器
     * @return
     */
    @GetMapping("/purchInstore")
    public String purchInstore(@RequestParam Long id, ModelMap mmap) {
        mmap.put("inOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.IN_ORDER.getPref()));
        //获取采购单信息
        mmap.put("inOrderVo",inOrderService.purchTransfor(id));
        return prefix + "/oneClickInstore";
    }

    /**
     * 生产一键入库
     * @param id 采购单主键ID
     * @param mmap 装数据的容器
     * @return
     */
    @GetMapping("/productInstore")
    public String productInstore(@RequestParam Long id, ModelMap mmap) {
        mmap.put("inOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.IN_ORDER.getPref()));
        //获取采购单信息
        mmap.put("inOrderVo",inOrderService.productTransfor(id));
        return prefix + "/oneClickInstore";
    }

    /**
     * 新增保存商品入库单主
     */
    @RequiresPermissions("store:inorder:add")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InOrderDto inOrderDto) {
        return toAjax(inOrderService.insertInOrder(inOrderDto));
    }

    /**
     * 修改商品入库单主
     */
    @RequiresPermissions("store:inorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        InOrderVo inOrderVo = inOrderService.selectInOrderById(id);
        mmap.put("inOrderVo", inOrderVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品入库单主
     */
    @RequiresPermissions("store:inorder:edit")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InOrderDto inOrderDto) {
        return toAjax(inOrderService.updateInOrder(inOrderDto));
    }

    /**
     * 删除商品入库单主
     */
    @RequiresPermissions("store:inorder:remove")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(inOrderService.deleteInOrderByIds(ids));
    }


    /**
     * 跳转到审核页面
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/checkDetail")
    public String checkDetail(Long id,ModelMap mmap) {
        InOrderVo inOrderVo = inOrderService.selectInOrderById(id);
        mmap.put("inOrderVo", inOrderVo);
        return prefix + "/checkDetail";
    }

    /**
     * 入库单审核
     */
    @RequiresPermissions("store:inorder:audit")
    @Log(title = "入库单审核", businessType = BusinessType.AUDIT)
    @PostMapping("/checkStatus")
    @ResponseBody
    public AjaxResult checkStatus(CheckOrderLog checkOrderLog) {
        return toAjax(inOrderService.checkStatus(checkOrderLog));
    }


}
